<?php
namespace app\admin\controller;
use app\admin\model\ImagesModel;
use app\admin\controller\Base;
use think\Controller;
use think\Request;
class  Images extends Base{

    //轮播图列表
    public function index(){
        $imgs = new ImagesModel();
        $imgs = $imgs->getImages();
        $page = $imgs->render();
        $this->assign("imgs",$imgs);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //删除图片
    public function delImages(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $del = new ImagesModel();
            $row = $del->delImages($id);
            return $row;
        }
    }

    //设置首页轮播图
    public function setHomeImages(){
        if(Request::instance()->isPost()){
            $id =input('id');
            $img = new ImagesModel();
            $row =$img->setHome($id);
            return $row;
        }else{
            return false;
        }
    }

    //设置商城轮播图
    public function setGoodsImages(){
        if(Request::instance()->isPost()){
            $id =input('id');
            $img = new ImagesModel();
            $row =$img->setGoods($id);
            return $row;
        }else{
            return false;
        }
    }
    //取消轮播图设置
    public function removeSet(){
        if(Request::instance()->isPost()){
            $id =input('id');
            $img = new ImagesModel();
            $row =$img->removeSetImages($id);
            return $row;
        }else{
            return false;
        }
    }

    //获取添加图片信息
    public function getReturnImg(){
        if(Request::instance()->isAjax())
        {
            return $this->upload();
        }else
        {
            return $this->fetch('upload_imgs');
        }
    }


    //添加图片
    public function addImage(){
        $imgs_url = input('imgs_url');
        $img_width = (int)input('img_width');
        $img_height = (int)input('img_height');
        $img_size =(int)input('img_size');
        $errorMsg =[
            'width'=>$img_width,
            'height'=>$img_height,
            'size'=>$img_size,
        ];
        if($img_width>=750&&$img_width<=1000&&$img_height>=300&&$img_height<=500&&$img_size<=307200){
            $data = [
                'imgs_url' => $imgs_url,
                'status'=> 0,
                'imgs_time'=>time()
            ];
            $img = new ImagesModel();
            $img->addImages($data);
            return 1;
        }else{
            return $errorMsg;
        }
    }
}